package com.zhuozhou.jdyc.main.base.adapter;

import android.content.Context;
import android.view.View;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.base.BaseRecyclerViewAdapter;
import com.zhuozhou.jdyc.base.BaseViewHolder;
import com.zhuozhou.jdyc.databinding.ImgAddItemBinding;
import com.zhuozhou.jdyc.util.log.LogUtil;
import com.zhuozhou.jdyc.util.log.ToastUtils;
import com.zhuozhou.jdyc.wight.picDialog.PicShowDialog;

/**
 * Created by 方舟 on 2017/10/20.
 * 添加圖片
 */
public class ImageAddAdapter extends BaseRecyclerViewAdapter<String,ImgAddItemBinding> {

    public ImageViewClearListener imageViewClearListener;
    public ImageViewAddListener imageViewAddListener;
    private int defaultMaxCount = 8;//最大上传数量

    public ImageAddAdapter(Context context) {
        super(context);
    }

    public ImageAddAdapter(Context context, int maxCount) {
        super(context);
        this.defaultMaxCount = maxCount;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.img_add_item;
    }

    @Override
    public void onBindHolder(BaseViewHolder<ImgAddItemBinding> holder, int pos) {
        holder.getBinding().ivClearImg.setVisibility(mList.size() == defaultMaxCount ? View.GONE : View.VISIBLE);
        if (pos == mList.size() && mList.size() < defaultMaxCount) {
            holder.getBinding().ivAdd.setOnClickListener(v -> {
                if (imageViewAddListener != null) {
                    imageViewAddListener.imgAdd(v);
                }
            });
            Glide.with(mContext).load(R.mipmap.ic_tweet_add).into(holder.getBinding().ivAdd);
            holder.getBinding().ivClearImg.setVisibility(View.GONE);
        } else {
            holder.getBinding().ivClearImg.setVisibility(View.VISIBLE);
            holder.getBinding().ivClearImg.setOnClickListener(v -> {
                if (imageViewClearListener != null) {
                    imageViewClearListener.imgClear(v, pos);
                }
            });
            holder.getBinding().ivAdd.setOnClickListener(v -> {
                try{
                    new PicShowDialog(mContext,PicShowDialog.createImageInfo(mList),pos).show();
                }catch (Exception e){
                    e.printStackTrace();
                    ToastUtils.showShort(mContext,"图片打开失败");
                }
            });
            LogUtil.show("图片地址："+mList.get(pos));
            Glide.with(mContext)
                    .load(mList.get(pos))
                    .apply(new RequestOptions().placeholder(R.mipmap.ic_default_image).error(R.mipmap.ic_default_image))
                    .into(holder.getBinding().ivAdd);
        }
    }

    public int getMaxCount() {
        return defaultMaxCount;
    }

    @Override
    public int getItemCount() {
        return super.getItemCount() == defaultMaxCount ? super.getItemCount() : super.getItemCount() + 1;
    }

    public void setImageViewClearListener(ImageViewClearListener imageViewClearListener) {
        this.imageViewClearListener = imageViewClearListener;
    }

    public interface ImageViewClearListener {
        void imgClear(View view, int position);
    }

    public void setImageViewAddListener(ImageViewAddListener imageViewAddListener) {
        this.imageViewAddListener = imageViewAddListener;
    }

    public interface ImageViewAddListener {
        void imgAdd(View view);
    }
}
